#include"login.h"
/* Usage:  */
int regi(int netfd,MYSQL*mysql,char*username,int*user_base_addr){
    TLV_t TLV;
    bzero(TLV.msg,PATHMAX);
    char p[1024]={0};
    sprintf(p,"select id from userdata where user = '%s'and is_deleted = 0 ;",username);
    mysql_query(mysql,p);
    MYSQL_RES*rows=mysql_store_result(mysql);
    if(mysql_num_rows(rows)!=0){
        int a=1;
        send(netfd,&a,sizeof(int),MSG_NOSIGNAL);
        return -1;
    }
    int a=0;
    send(netfd,&a,sizeof(int),MSG_NOSIGNAL);
    //生成盐值
char salt[21]={0};
salt[0]='$';
salt[1]='6';
salt[2]='$';
salt[19]='$';
char val[]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
srand(time(NULL));
for(int i=3;i<19;i++){
    salt[i]=val[rand()%64];
}
//给客户端发送盐值
TLV_t salt_t;
bzero(salt_t.msg,PATHMAX);
salt_t.type=0;
salt_t.len=strlen(salt);
memcpy(salt_t.msg,salt,salt_t.len);
send(netfd,&salt_t,sizeof(int)*2+salt_t.len,MSG_NOSIGNAL);
char *password;
//接收服务端的密文
recv(netfd,&TLV.type,sizeof(int),MSG_WAITALL);
recv(netfd,&TLV.len,sizeof(int),MSG_WAITALL);
recv(netfd,TLV.msg,TLV.len,MSG_WAITALL);
password=TLV.msg;
bzero(p,strlen(p));
    sprintf(p,"insert into userdata (user,salt,password,is_deleted) value('%s','%s','%s',0);",username,salt,password);
    mysql_query(mysql,p);
    bzero(p,strlen(p));
    sprintf(p,"insert into vft (filename,user,parent_id,type,path)value('%s','%s',-1,'d','/%s');",username,username,username);
    mysql_query(mysql,p);
bzero(p,strlen(p));
sprintf(p,"select id from vft where filename = '%s'and path = '/%s';",username,username);
     my_ulonglong insert_id = mysql_insert_id(mysql);
     *user_base_addr=(int)insert_id;
    return 0;
}
int login(int netfd,MYSQL *mysql,char*username,int *user_base_addr){
    TLV_t TLV;
    bzero(TLV.msg,PATHMAX);
    char *p=(char*)calloc(1024,sizeof(char));
    sprintf(p,"select salt,password from userdata where user = '%s'and is_deleted = 0;",username);
    mysql_query(mysql,p);
    //用户名不存在，返回-1
    MYSQL_RES*rows=mysql_store_result(mysql);
    if(mysql_num_rows(rows)==0){
        int a=-1;
        send(netfd,&a,sizeof(int),0);
            return -1;
    }
    //存在用户名发送1
    int a=1;
    send(netfd,&a,sizeof(int),0);
    //发送盐值
        MYSQL_ROW row=mysql_fetch_row(rows);
        TLV_t salt;
        bzero(salt.msg,PATHMAX);
        salt.len=strlen(row[0]);
        memcpy(salt.msg,row[0],salt.len);
        send(netfd,&salt,sizeof(int)*2+salt.len,MSG_NOSIGNAL);
        int res=recv(netfd,&TLV.type,sizeof(int),MSG_NOSIGNAL);
        if(res==0){
            return -1;
        }
        recv(netfd,&TLV.len,sizeof(int),MSG_WAITALL);
        recv(netfd,TLV.msg,TLV.len,MSG_WAITALL);
        int access=1;
        if(strcmp(TLV.msg,row[1])!=0){
           access=0;
            send(netfd,&access,sizeof(int),MSG_NOSIGNAL);
            return -1;
        }
            send(netfd,&access,sizeof(int),MSG_NOSIGNAL);
    //返回用户根目录的id
    bzero(p,strlen(p));
    sprintf(p,"select id from vft where path = '/%s';",username);
    mysql_query(mysql,p);
    MYSQL_RES *rows2=mysql_store_result(mysql);
    MYSQL_ROW row2=mysql_fetch_row(rows2);
*user_base_addr=atoi(row2[0]);
free(p);
    return 0;

}

